﻿/*

@Name：不落阁整站模板源码 
@Author：Absolutely 
@Site：http://www.lyblogs.cn

*/

layui.use(['element', 'jquery', 'form', 'layedit','flow'], function () {
    var element = layui.element;
    var form = layui.form;
    var $ = layui.jquery;
    var layedit = layui.layedit;
    var flow = layui.flow;

    //评论和留言的编辑器
    // var editIndex = layedit.build('remarkEditor', {
    //     height: 150,
    //     tool: ['face', '|', 'left', 'center', 'right', '|', 'link'],
    // });
    //评论和留言的编辑器的验证
    layui.form.verify({
        content: function (value) {
            value = $("#remarkEditor").val();
            if (value == "") return "自少得有一个字吧";
           // layedit.sync(editIndex);
        }
    });

    axios.get("/personal_blog/links").then(function(res){
    	var beans = res.data;
	    for(var i in beans){
	    	var li = $("<li></li>");
	    	var a = $('<a target="_blank" href="'+beans[i].linkUrl+'" title="'+beans[i].linkName+'"  class="friendlink-item"></a>');
	    	var p = $('<p class="friendlink-item-pic"><img src="'+beans[i].linkUrl+'/favicon.ico " alt="'+beans[i].linkName+'" /></p>');
	    	a.append(p);
	    	p = $('<p class="friendlink-item-title">'+beans[i].linkName+'</p>');
	    	a.append(p);
	    	var url = beans[i].linkUrl;
	    	if(url.indexOf("http://")==0){
	    		url = url.substring(7);
	    	}else if(url.indexOf("https://")==0){
	    		url = url.substring(8);
	    	}
	    	if(url.indexOf("/") > 0){
	    		url = url.substring(0,url.indexOf("/"));
	    	}
	    	p = $('<p class="friendlink-item-domain">'+url+'</p>');
	    	a.append(p);
	    	li.append(a);
	    	$(".friendlink").append(li);
	    }
    })
    
    var pageSize = 10;
    var pageIndex = 0;
    
    flow.load({
        elem: '#bbs',//存放每条数据的容器
        done: function (pageIndex, next) { //到达临界点（默认滚动触发），触发下一页
            var lis = [];
            $.get('/personal_blog/bbses/listByPage/reviewed?page='+pageIndex+"&limit="+pageSize, function(res){
                layui.each(res.data, function(index, item){
                	var j = Math.ceil(Math.random()*20);
                	var bbsli = '<li><div class="comment-parent">';
                	bbsli += '<svg class="icon-comment" aria-hidden="true"><use xlink:href="#icon-'+j+'"></use></svg>';
                	bbsli += '<div class="info"><span class="username">'+item.bbsUserIp+'</span></div>';
                	bbsli += '<div class="content">'+item.bbsContent+'</div>';
                	bbsli += '<p class="info info-footer"><span class="time">'+moment(item.bbsDate).format('YYYY-MM-DD HH:mm:ss')+'</span>';
                	//bbsli += '<a class="btn-reply" href="javascript:;" onclick="btnReplyClick(this)">回复</a>';
                    bbsli += '</p></div>';
                  //  bbsli += '<hr />';
                    lis.push(bbsli);
                	
                });
                next(lis.join(''), pageIndex < res.pages);
            })
        }
    });
    
    
    
    
    
    //Hash地址的定位
    var layid = location.hash.replace(/^#tabIndex=/, '');
    if (layid == "") {
        element.tabChange('tabAbout', 1);
    }
    element.tabChange('tabAbout', layid);

    element.on('tab(tabAbout)', function (elem) {
        location.hash = 'tabIndex=' + $(this).attr('lay-id');
    });

    //监听留言提交
    form.on('submit(formLeaveMessage)', function (data) {
        var index = layer.load(1);
        //模拟留言提交
        setTimeout(function () {
            layer.close(index);
            var content = data.field.editorContent;
            console.log(content)
            var url = "/personal_blog/bbses/addBbs";

            axios.post(url,{
                bbsContent:content
            }).then(function (res) {
                $('#remarkEditor').val('');

                layer.msg("留言已成功提交后台，审核后可显示", { icon: 1 });
            })






        }, 500);
        return false;
    });

    //监听留言回复提交
    form.on('submit(formReply)', function (data) {
        var index = layer.load(1);
        //模拟留言回复
        setTimeout(function () {
            layer.close(index);
            var content = data.field.replyContent;
            var html = '<div class="comment-child"><img src="../images/Absolutely.jpg"alt="Absolutely"/><div class="info"><span class="username">模拟回复</span><span>' + content + '</span></div><p class="info"><span class="time">2017-03-18 18:26</span></p></div>';
            $(data.form).find('textarea').val('');
            $(data.form).parent('.replycontainer').before(html).siblings('.comment-parent').children('p').children('a').click();
            layer.msg("回复成功", { icon: 1 });
        }, 500);
        return false;
    });
});
function btnReplyClick(elem) {
    var $ = layui.jquery;
    $(elem).parent('p').parent('.comment-parent').siblings('.replycontainer').toggleClass('layui-hide');
    if ($(elem).text() == '回复') {
        $(elem).text('收起')
    } else {
        $(elem).text('回复')
    }
}
systemTime();

function systemTime() {
    //获取系统时间。
    var dateTime = new Date();
    var year = dateTime.getFullYear();
    var month = dateTime.getMonth() + 1;
    var day = dateTime.getDate();
    var hh = dateTime.getHours();
    var mm = dateTime.getMinutes();
    var ss = dateTime.getSeconds();

    //分秒时间是一位数字，在数字前补0。
    mm = extra(mm);
    ss = extra(ss);

    //将时间显示到ID为time的位置，时间格式形如：19:18:02
    document.getElementById("time").innerHTML = year + "-" + month + "-" + day + " " + hh + ":" + mm + ":" + ss;
    //每隔1000ms执行方法systemTime()。
    setTimeout("systemTime()", 1000);
}

//补位函数。
function extra(x) {
    //如果传入数字小于10，数字前补一位0。
    if (x < 10) {
        return "0" + x;
    }
    else {
        return x;
    }
}